package com.zrf.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.zrf.gulimall.order.entity.OrderEntity;
import com.zrf.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author zhanrf
 * @Date 2022/1/8 23:32
 */

@RabbitListener(queues = "order.release.queue")
@Component
@Slf4j
public class OrderCloseListener {
	@Autowired
	private OrderService orderService;

	@RabbitHandler
	public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
		log.info("收到过期的订单信息，准备关闭订单{}", orderEntity.getOrderSn());
		try {
			orderService.closeOrder(orderEntity);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (Exception e) {
			channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
		}

	}
}
